import os
from multiprocessing import Pool

class RunManager:

    def __init__(self):
        os.system("python -V")  # 输出 Python 版本
        pass
        folder_path = 'e:/report/result'
        if len(os.listdir(folder_path)) > 0:
            for file_name in os.listdir(folder_path):
                file_path = os.path.join(folder_path, file_name)
                print("file_path",file_path)
                os.remove(file_path)

    def run_pytest(self,file):
        cmd = f"pytest {os.path.abspath(file)} --alluredir e:/report/result"
        print(os.path.abspath(file))
        # cmd = f"pytest {os.path.abspath(file)} --alluredir "+ alluredir
        print("执行命令",cmd)
        os.system(cmd)
    def run_py(self,file):
        cmd = f"python {os.path.abspath(file)}"
        print(os.path.abspath(file))
        # cmd = f"pytest {os.path.abspath(file)} --alluredir "+ alluredir
        print("执行命令", cmd)
        os.system(cmd)
    def run_and_create_report(self):

        # 获取当前目录
        current_dir =  os.path.join(os.path.dirname(os.path.abspath(__file__)),"../testcases")
        current_dir = os.path.abspath(current_dir)  # 规范化路径
        print(current_dir)
        # 列出所有 test_ 开头的 .py 文件
        test_files = [
            os.path.join(current_dir, f)
            for f in os.listdir(current_dir)
            if f.startswith("test_") and f.endswith(".py")
        ]
        print(" 找到以下测试文件：")
        for file in test_files:
            print(" -", file)
            self.run_pytest(file)
        # 执行 图片转gif
        self.run_py(os.path.join(current_dir,"images_to_gif.py"))
        # with Pool(3) as pool:
        #     pool.map(self.run_pytest, test_files)
        # os.path.abspath(__file__)
        os.system('allure generate --clean e:/report/result -o e:/report/html')
        # os.system("allure generate %s -o %s --clean" % (cm.dir_report_json, cm.dir_report_html))


if __name__ == '__main__':
    RunManager().run_and_create_report()




















